from factor_analyzer.factor_analyzer import calculate_kmo, calculate_bartlett_sphericity


def kmo_bartlett(df):
    """
        params:     df -> python的dataframe
        return:     kmo -> KMO值
                    bt_kf -> Bartlett球形度检验的卡方值
                    free -> 自由度
                    p -> Bartlett球形度检验的P值
    """
    # 计算KMO值
    kmo_all, kmo_model = calculate_kmo(df)
    chi_square_value, p_value = calculate_bartlett_sphericity(df)
    # 计算自由度
    p = df.shape[1]
    dof = p * (p - 1) / 2
    m = dict()
    m["kmo"] = kmo_model  # KMO值
    m["bt_kf"] = chi_square_value  # Bartlett球形度检验的卡方值
    m["free"] = dof  # 自由度
    m["p"] = p_value  # Bartlett球形度检验的P值
    return m
